package com.srxz.personnelsystem.conter.controller;

import com.srxz.personnelsystem.util.BackMessage;
import com.srxz.personnelsystem.util.UnauthorizedException;
import org.apache.shiro.ShiroException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: ExceptionController
 * @author: wgchao
 * @createTime: 2019/1/23 3:05 PM
 * @Description: TODO
 */
@RestControllerAdvice
public class ExceptionController {
//    // 捕捉shiro的异常
//    @ResponseStatus(HttpStatus.UNAUTHORIZED)
//    @ExceptionHandler(ShiroException.class)
//    public BackMessage handle401(ShiroException e) {
//        return BackMessage.error("shiro的异常");
//    }
//
    // 捕捉UnauthorizedException
//    @ExceptionHandler(UnauthorizedException.class)
//    public String handle401() {
//        return "login";
//    }
//
//    // 捕捉其他所有异常
//    @ExceptionHandler(Exception.class)
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
//    public BackMessage globalException(HttpServletRequest request, Throwable ex) {
//        return BackMessage.error(ex.getMessage());
//    }
//
//    private HttpStatus getStatus(HttpServletRequest request) {
//        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
//        if (statusCode == null) {
//            return HttpStatus.INTERNAL_SERVER_ERROR;
//        }
//        return HttpStatus.valueOf(statusCode);
//    }

    @ExceptionHandler({Exception.class})
    public BackMessage handException(HttpServletRequest request ,Exception e) throws Exception{
      //  log.error(message);
        e.printStackTrace();
        return BackMessage.error(e.getMessage());
    }
}
